<?php
/**
 * @version		1.0.0
 * @category	Anahita Social Simple Blogs
 * @copyright	Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
 * @link     	http://www.anahitapolis.com
 */

/**
 * Blog Post Controller
 *
 */
class ComSblogsControllerPost extends AnSekitMediumController
{
	
    /**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param 	object 	An optional KConfig object with configuration options.
     * @return 	void
     */
	protected function _initialize(KConfig $config)
	{
		$config->append(array(
			'publish_story_after' => array('add')
		));
	
		parent::_initialize($config);
	}
		
	/**
	 * Browse a list of posts
	 * 
	 * @see AnSekitController::_actionBrowse()
	 */
	public function _actionBrowse($context)
	{
	     $data = $context->data;
	     $owner = $data->actor ; //the owner fetched from the oid happend in the AnSekitMediumController
	     $owner->posts
//	     	->where('title','LIKE','%somestring%') //filters the posts title by somestring
//	     	->order('creationTime','DESC')
	     	; // order them by creationTime asc	     
	}
	
	/**
	 * Adds a post
	 *
	 * @param KCommandContext $context
	 */
	protected function _actionAdd($context)
	{
		$data  = $context->data;
		$owner = $data->actor;
		$post  = $data->actor->posts->add(array(
			'application'		=> $this->getApplication(),
			'author'			=> $this->getViewer(),
			'openToComment'		=> true,
			'owner'				=> $owner,
			'title'				=> $data->property->title ,
			'body'				=> $data->property->body
		));
		
		$data->post = $post;
		
		if( AnDomain::getContext()->save() )
		{
			$this->setRedirect($post->getURL(), JText::_('AN-SBLOGS-POST-ADD-SUCCESS-PROMPT'));
			return true;
		}
		
		return false;
	}
	
	/**
	 * Publishes a new post story after it's creation
	 *
	 * @param  KCommandContext $context
	 * @return void
	 */
	public function publishStory(KCommandContext $context)
	{
		$post  = $context->data->post;		
		$story = $this->getApplication()->publishNewPostStory($post);
	}
}
 